#include "event_manager.h"

GraphicEngineEventManager::GraphicEngineEventManager(CameraHandle& handle_camera)
	: ref_handle_camera(handle_camera)
	, mouse_state()
{

}

bool GraphicEngineEventManager::OnEvent(const irr::SEvent& event)
{
	if( event.EventType == irr::EET_MOUSE_INPUT_EVENT )
	{
		switch(event.MouseInput.Event)
		{
		case irr::EMIE_LMOUSE_PRESSED_DOWN: mouse_state.LeftButtonDown  = true; break;
		case irr::EMIE_RMOUSE_PRESSED_DOWN: mouse_state.RightButtonDown = true; break;
		case irr::EMIE_LMOUSE_LEFT_UP:      mouse_state.LeftButtonDown  = false; break;
		case irr::EMIE_RMOUSE_LEFT_UP:      mouse_state.RightButtonDown = false; break;
		case irr::EMIE_MOUSE_MOVED:
			{
				if(mouse_state.LeftButtonDown && !mouse_state.RightButtonDown)
				{
					ref_handle_camera.turn( 
						irr::f32(event.MouseInput.X - mouse_state.oldPosition.X),
						irr::f32(event.MouseInput.Y - mouse_state.oldPosition.Y));
				}
				else if (!mouse_state.LeftButtonDown && mouse_state.RightButtonDown)
				{
					ref_handle_camera.moveLU( 
						irr::f32(event.MouseInput.X - mouse_state.oldPosition.X),
						irr::f32(event.MouseInput.Y - mouse_state.oldPosition.Y));
				}
				mouse_state.oldPosition.X = event.MouseInput.X;
				mouse_state.oldPosition.Y = event.MouseInput.Y;
			}
			break;
		case irr::EMIE_MOUSE_WHEEL:
			{
				irr::f32 sensibility(-10.0);
				ref_handle_camera.moveLF(0, sensibility * event.MouseInput.Wheel);
			}
			break;
		default: break;
		}
	}
	return false;
}